import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import '../../../../lib_runtime/utils/string_util.dart';
import '../../../../resource/language/generated/l10n.dart';
import '../../../m_chatroom.dart';
import '../mic_seat/mic_seat_widget_model.dart';
import 'mic_resource_setting_page_model.dart';
import 'mic_select_page_model.dart';
import 'widget/doufeng_mic_number_label.dart';
// import 'package:lib_baseview/lib_baseview.dart';

//麦克风Widget

@Deprecated("Use RoomMic instead")
class MicSeatItemWidget extends StatefulWidget {
  static const msgShowMicImage = 'msg_show_mic_image';

  // final SimpleChannelUserSlot? item;
  final VoidCallback? onPressed;
  final double width;
  final TopRightLabel? topRightLabel;
  final TitlePrefix? titlePrefix;
  final int position;

  MicSeatItemWidget(
      {super.key,
      // this.item,
      this.onPressed,
      this.width = 50,
      this.topRightLabel,
      this.titlePrefix,
      this.position = 0});

  @override
  State<MicSeatItemWidget> createState() => _MicSeatItemWidgetState();
}

class _MicSeatItemWidgetState extends State<MicSeatItemWidget> {
  String? roleTitle;

  MicSeatWidgetModel? model;
  MicSelectPageModel? micSelectPageModel;

  @override
  void initState() {
    super.initState();
  }

  Widget? get placeHolder {
    Widget? emptyMicSeat;

    // if (model?.micSeatScene == MicSeatScene.WAITING_LIST ||
    //     model?.micSeatScene == MicSeatScene.SEAT_FRAME_SETTING) {
    //   if (widget.item?.hasUser() == false) {
    //     emptyMicSeat = Container(
    //       width: widget.width,
    //       height: widget.width,
    //       alignment: Alignment.center,
    //       decoration: BoxDecoration(
    //         color: (micSelectPageModel?.selectedSeat != null &&
    //                 micSelectPageModel?.selectedSeat == widget.item &&
    //                 micSelectPageModel?.selectedPosition == widget.position)
    //             ? Color(0xFFFFFEDB)
    //             : Color(0xFFF6F7F9),
    //         borderRadius: BorderRadius.circular(widget.width / 2),
    //       ),
    //       child: Text(
    //         roleTitle ?? "",
    //         style: TextStyle(
    //             fontSize: 11,
    //             fontWeight: FontWeight.w500,
    //             color: (micSelectPageModel?.selectedSeat != null &&
    //                     micSelectPageModel?.selectedSeat == widget.item &&
    //                     micSelectPageModel?.selectedPosition == widget.position)
    //                 ? Color(0xFFD18D49)
    //                 : Color(0xFF666666),
    //             height: 1),
    //       ),
    //     );
    //   }
    // }
    // return emptyMicSeat;
    return Text('placeHolder');
  }

  Widget? get topRightLabel {
    if (widget.topRightLabel is NumTopRightLabel) {
      NumTopRightLabel numTopRightLabel =
          widget.topRightLabel as NumTopRightLabel;
      return DoufengMicNumberLabel(num: numTopRightLabel.num);
    }
    return null;
  }

  Widget _buildContent(
      BuildContext context, MicSeatWidgetModel micSeatWidgetModel) {
    model = micSeatWidgetModel;
    MicResourceSettingPageModel? micResourceSettingPageModel;
    bool needShowMicStatus = false;
    Color textColor = Colors.white;
    // SimpleChannelUserSlot? slot = widget.item;
    // bool isRide = model?.channel?.isDouFeng ?? false;
    // bool isHotHall = model?.channel?.isHotHall ?? false;
    bool isRide = false;
    bool isHotHall = false;
    var onPressed = widget.onPressed;

    // switch (slot?.micType) {
    //   case MicTypeEnum.CAR_OWNER_MIC:
    //     roleTitle = T.current!.driver;
    //     break;
    //   case MicTypeEnum.COMMON_MIC:
    //     if (isRide) {
    //       roleTitle = T.current!.guest;
    //     }
    //     break;
    //   case MicTypeEnum.HOST_MIC:
    //     roleTitle = T.current!.host;
    //     break;
    // }
    // if (model?.micSeatScene == MicSeatScene.WAITING_LIST) {
    //   int micIndex = widget.position + 1;
    //   if (isHotHall) {
    //     micIndex = widget.position - 2;
    //   }
    //   roleTitle = roleTitle ?? T.current!.mic_position(micIndex);
    //   textColor = const Color(0xff333333);
    //   micSelectPageModel = context.watch<MicSelectPageModel>();
    //   if (widget.item == null || widget.item?.hasUser() == false) {
    //     onPressed ??= () {
    //       micSelectPageModel?.selectSeat(widget.item, widget.position);
    //     };
    //   }
    // } else if (model?.micSeatScene == MicSeatScene.SEAT_FRAME_SETTING) {
    //   textColor = const Color(0xff333333);
    //   int micIndex = widget.position + 1;
    //   if (isHotHall) {
    //     micIndex = widget.position - 2;
    //   }
    //   roleTitle = roleTitle ?? T.current!.mic_position(micIndex);
    //   micResourceSettingPageModel =
    //       context.watch<MicResourceSettingPageModel>();
    //   onPressed ??= () {
    //     micResourceSettingPageModel?.openMicSourceListPage(
    //         context, widget.position);
    //   };
    // } else if (model?.micSeatScene == MicSeatScene.ROOM) {
    //   needShowMicStatus = true;
    //   if (widget.position == 0 && (model?.channel?.showRoomOwner ?? false)) {
    //     roleTitle = T.current!.homeowner;
    //   }
    //   if (widget.item != null) {
    //     onPressed ??= () {
    //       if (widget.item?.user != null &&
    //           (widget.item?.user.id.toInt() ?? 0) > 0) {
    //         RoomPersonalDataWidget.show(
    //             context, model?.channel, widget.item!.user,
    //             secretUser: widget.item!.user.secretUser == 1);
    //       } else {
    //         model?.roomModel.joinMicWithPosition(context, widget.position);
    //       }
    //     };
    //   }
    // }
    return CupertinoButton(
      onPressed: onPressed,
      minSize: 0,
      padding: EdgeInsets.zero,
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          MicAvatarWidget(
            // item: widget.item,
            position: widget.position,
            width: widget.width,
            empty: placeHolder,
            // micSeatScene: model?.micSeatScene ?? MicSeatScene.ROOM,
            onPressed: onPressed,
            rightLabel: topRightLabel,
          ),
          const SizedBox(
            height: 6,
          ),
          SizedBox(
              height: 12,
              width: widget.width,
              child: OverflowBox(
                maxWidth: widget.width * 1.6,
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    _getTitlePrefixWidget(),
                    Container(
                      constraints: BoxConstraints(maxWidth: widget.width * 1.3),
                      child: Text(
                        // StringUtil.removeHideSpace(
                        //     widget.item?.user.nickname ?? ''),
                        'widget.item?.user.nickname',
                        softWrap: false,
                        maxLines: 1,
                        overflow: TextOverflow.ellipsis,
                        textAlign: TextAlign.center,
                        style: TextStyle(
                            fontSize: 9,
                            fontWeight: FontWeight.w400,
                            color: textColor),
                      ),
                    ),
                  ],
                ),
              )),
          // needShowMicStatus && widget.item?.hasUser() == true
          //     ? Container(
          //         decoration: const ShapeDecoration(
          //           shape: StadiumBorder(),
          //           color: Color(0x14FFFFFF),
          //           // color: Colors.red,
          //         ),
          //         margin: const EdgeInsets.only(top: 4),
          //         padding: const EdgeInsets.symmetric(horizontal: 4),
          //         height: 14,
          //         child: Row(
          //           mainAxisSize: MainAxisSize.min,
          //           children: [
          //             /// 主播头像下面的麦克图片
          //             if (widget.item != null)
          //               image(
          //                 widget.item?.micStatus == MicStatus.OPEN
          //                     ? "ic_mic_on_wheat.svg"
          //                     : "ic_mic_off_wheat.svg",
          //                 width: 7,
          //                 // height: 20,
          //                 fit: BoxFit.cover,
          //               ),
          //             if (counterTime > 0)
          //               Row(
          //                 mainAxisSize: MainAxisSize.min,
          //                 children: [
          //                   const SizedBox(
          //                     width: 4,
          //                   ),
          //                   Container(
          //                     width: 0.5,
          //                     height: 7,
          //                     color: Colors.white.withOpacity(0.3),
          //                   ),
          //                   const SizedBox(
          //                     width: 4,
          //                   ),
          //                   Text(
          //                     '${counterTime}s',
          //                     style: JRTextStyles.styleFont9W400,
          //                   )
          //                 ],
          //               ),
          //             if (roleTitle != null && counterTime <= 0) ...[
          //               const SizedBox(
          //                 width: 2,
          //               ),
          //               Text(
          //                 roleTitle!,
          //                 style: const TextStyle(
          //                     fontSize: 8,
          //                     fontWeight: FontWeight.w500,
          //                     color: Colors.white,
          //                     height: 1),
          //               ),
          //             ],
          //           ],
          //         ),
          //       )
          //     : const SizedBox(height: 18),
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Consumer<MicSeatWidgetModel>(
      builder: (context, model, child) => _buildContent(context, model),
    );
  }

  ///倒计时
  int get counterTime {
    // int time = 0;
    // if (widget.item?.hasUser() ?? false) {
    //   int timeNow = GlobalConfig.serverTime ~/ 1000;
    //   time = widget.item!.counterTimestamp - timeNow;
    // }
    return 10;
  }

  Widget _getTitlePrefixWidget() {
    if (widget.titlePrefix == null) {
      return Container();
    }
    if (widget.titlePrefix is NumTitlePrefix) {
      NumTitlePrefix numTitlePrefix = widget.titlePrefix as NumTitlePrefix;
      return Container(
        height: 9,
        width: 9,
        margin: EdgeInsets.only(right: 2),
        alignment: Alignment.center,
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(4.5),
          color: Color(0x14FFFFFF),
        ),
        child: Text(
          // "${(topRightLabel as ContributionTopRightLabel).contribution}",
          "${numTitlePrefix.num}",
          style: TextStyle(
              fontSize: 6,
              fontWeight: FontWeight.w400,
              color: Colors.white,
              height: 1),
        ),
      );
    }
    return Container();
  }

//  千转换成K
  String roomHeatTreatment(int? heat) {
    if (heat! > 1000) {
      double tmpHeat = heat / 1000;
      tmpHeat.toStringAsFixed(1);

      return tmpHeat.toStringAsFixed(1) + "k";
    } else {
      return heat.toString();
    }
  }
}

mixin TopRightLabel {}

class NumTopRightLabel with TopRightLabel {
  int num;

  NumTopRightLabel({required this.num});
}

class ContributionTopRightLabel with TopRightLabel {
  int contribution;

  ContributionTopRightLabel({required this.contribution});
}

mixin TitlePrefix {}

class NumTitlePrefix with TitlePrefix {
  int num;

  NumTitlePrefix({required this.num});
}

class TestVoiceLabel extends StatelessWidget {
  const TestVoiceLabel({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: const EdgeInsets.only(bottom: 5),
      padding: const EdgeInsets.symmetric(vertical: 1, horizontal: 2),
      decoration: const ShapeDecoration(
          gradient:
              LinearGradient(colors: [Color(0xFFFF6552), Color(0xFFFF5F00)]),
          shape:
              StadiumBorder(side: BorderSide(color: Colors.white, width: 0.5))),
      child: Text(
        T.current!.talent_show,
        style: TextStyle(
            color: Colors.white, fontSize: 8, fontFamily: 'youshebiaotihei'),
      ),
    );
  }
}
